#!/usr/bin/env python
# -*- coding:utf-8 -*-

from functools import wraps
from flask import request, jsonify
from utils.jwt_util import JwtToken
from config import Config  # 从config包导入Config类

def login_required(func):
    """
    登录验证装饰器
    验证用户是否登录（token是否有效）
    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        # 获取token
        auth_header = request.headers.get('Authorization')
        if not auth_header:
            return jsonify({
                "code": "4008",
                "message": "未提供token"
            })
        
        # 处理Bearer token
        try:
            token = auth_header.split(' ')[1] if 'Bearer' in auth_header else auth_header
        except IndexError:
            return jsonify({
                "code": "4008",
                "message": "token格式错误"
            })
        
        # 获取JWT配置
        jwt_config = Config.get_jwt_config()
        
        # 验证token
        verify_status, payload = JwtToken.parse_token(token, jwt_config['secret_key'])
        if not verify_status:
            return jsonify({
                "code": "4006",
                "message": payload.get('err', 'token验证失败')
            })
        
        # 将用户信息添加到请求上下文
        request.user = payload
        return func(*args, **kwargs)
    return wrapper

def counselor_required(func):
    """
    咨询师权限验证装饰器
    验证用户是否是咨询师
    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        # 获取token
        auth_header = request.headers.get('Authorization')
        if not auth_header:
            return jsonify({
                "code": "4008",
                "message": "未提供token"
            })
        
        # 处理Bearer token
        try:
            token = auth_header.split(' ')[1] if 'Bearer' in auth_header else auth_header
        except IndexError:
            return jsonify({
                "code": "4008",
                "message": "token格式错误"
            })
        
        # 获取JWT配置
        jwt_config = Config.get_jwt_config()
        
        # 验证token
        verify_status, payload = JwtToken.parse_token(token, jwt_config['secret_key'])
        if not verify_status:
            return jsonify({
                "code": "4006",
                "message": payload.get('err', 'token验证失败')
            })
        
        # 验证用户类型
        if payload.get('user_type') != 2:
            return jsonify({
                "code": "4009",
                "message": "需要咨询师权限"
            })
        
        # 将用户信息添加到请求上下文
        request.user = payload
        return func(*args, **kwargs)
    return wrapper

def client_required(func):
    """
    咨询者权限验证装饰器
    验证用户是否是咨询者
    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        # 获取token
        auth_header = request.headers.get('Authorization')
        if not auth_header:
            return jsonify({
                "code": "4008",
                "message": "未提供token"
            })
        
        # 处理Bearer token
        try:
            token = auth_header.split(' ')[1] if 'Bearer' in auth_header else auth_header
        except IndexError:
            return jsonify({
                "code": "4008",
                "message": "token格式错误"
            })
        
        # 获取JWT配置
        jwt_config = Config.get_jwt_config()
        
        # 验证token
        verify_status, payload = JwtToken.parse_token(token, jwt_config['secret_key'])
        if not verify_status:
            return jsonify({
                "code": "4006",
                "message": payload.get('err', 'token验证失败')
            })
        
        # 验证用户类型
        if payload.get('user_type') != 1:
            return jsonify({
                "code": "4010",
                "message": "需要咨询者权限"
            })
        
        # 将用户信息添加到请求上下文
        request.user = payload
        return func(*args, **kwargs)
    return wrapper 